var pmd = require('../../template/roll/roll.js');
var app = getApp();
var allReqEnd;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperinfo: {
      hgt: 0,
      indicatorDots: true,
      autoplay: false,
      dotcolor: 'rgba(0,0,0,0.3)',
      dotactivecolor: '#ffab3a',
      circular: true,
      interval: 5000,
      duration: 500,
    },
    showModel: false,
    banners: [],
    pmd: "0",
    swiperCurrent: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var swiper_hgt = 420 * res.windowWidth / 750;
        var swiperinfo = that.data["swiperinfo"];
        swiperinfo.hgt = swiper_hgt;
        that.setData({
          swiperinfo: swiperinfo,
          win_width: res.windowWidth
        });
      },
    });

    this.loadData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.pmd == '1') {
      pmd.run1();
    }
    if (app.globalData.needReqAfterUpdateAddress == true) {
      app.globalData.needReqAfterUpdateAddress = false;
      this.loadData();
    }
  },
  loadData: function () {
    allReqEnd = 0;
    wx.showLoading({
      title: '',
    });
    this.loadNotice();
    this.loadIndex();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    pmd.stopRun1();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // console.log("bottom");
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  agreeGetUser: function (e) {
    //设置用户信息本地存储
    try {
      wx.setStorageSync('userInfo', e.detail.userInfo);
    }
    catch (e) {
      wx.showToast({
        title: '系统提示:网络错误',
        icon: 'warn'
      });
    }
    let that = this;
    that.setData({
      showModel: false
    });
    app.getOP(e.detail.userInfo);
  },
  swiperChange: function (e) {
    if (e.detail.source == "touch") {
      //防止swiper控件卡死  
      if (this.data.current == 0) {// 卡死时，重置current为正确索引
        this.setData({
          swiperCurrent: 0
        });
      }
      else {//正常轮转时，记录正确页码索引  
        this.setData({
          swiperCurrent: e.detail.current
        });
      }
    }
  },
  dotTap: function (e) {
    var index = e.currentTarget.id;
    this.setData({
      swiperCurrent: index
    });
  },
  paomadeng: function () {
    pmd.run1();
  },
  // ------------------------加载公告-----------------------
  loadNotice: function () {
    if (app.globalData.address == null) {
      console.log('load notice');
      var that = this;
      var timeid = setTimeout(function () {
        clearTimeout(timeid);
        that.loadNotice();
      }, 250);
    }
    else {
      var url = '/yuanmai_we/weapp/banner/get/';
      url = url + app.globalData.address.CITYID;
      app.loadDataFromServer(url, this.loadNoticeCallBack, {}, 'get', true);
    }
  },
  loadNoticeCallBack: function (res) {
    var banners = [];
    var newproducts = [];
    var pintuan = null, kanjia = null;
    for(var i=0;i<res.length;i++){
      if (res[i].bannertype == 0){
        this.setData({
          pmd: '1'
        });
        pmd.rollInit(this, res[i].bannername);
      }
      else if (res[i].bannertype == 1){
        banners.push(res[i]);
      }
      else if (res[i].bannertype == 2){
        newproducts.push(res[i]);
      }
      else if (res[i].bannertype == 3){
        pintuan = res[i];
      }
      else if (res[i].bannertype == 4){
        kanjia = res[i];
      }
    }
    
    this.setData({
      banners: banners,
      newproducts: newproducts,
      pintuan: pintuan,
      kanjia: kanjia
    });
    this.paomadeng();
    this.checkRequestEnd();
  },
  //--------------------加载热销商品-------------------
  loadIndex: function () {
    if (app.globalData.address == null) {
      console.log('load index');
      var that = this;
      var timeid = setTimeout(function () {
        clearTimeout(timeid);
        that.loadIndex();
      }, 150);
    }
    else {
      var url = '/yuanmai_we/product/hot/banner/';
      url = url + app.globalData.address.CITYID;
      app.loadDataFromServer(url, this.loadIndexCallBack, {}, 'get', true);
    }
  },
  loadIndexCallBack: function (res) {
    console.log(res);
    this.setData({
      goodsinfos: res
    });
    this.checkRequestEnd();
  },
  checkRequestEnd: function () {
    allReqEnd++;
    if (allReqEnd == 2) {
      wx.hideLoading();
    }
  },
  //--------------------------跳转 某个热销商品---------------------------
  toGoods: function (event) {
    var goodsid = event.currentTarget.dataset.goodsid;
    goodsid = '252233';
    
    var menutype = event.currentTarget.dataset.menutype;
    if (menutype == 'NORMAL') {
      goodsid = "bread" + goodsid;
      app.globalData.tempBreadIndex = goodsid;
      wx.switchTab({
        url: '../bread/breadindex',
      });
    }
    else if(menutype == 'mcake'){
      goodsid = "cake" + goodsid;
      app.globalData.tempCakeIndex = goodsid;
      wx.switchTab({
        url: '../cake/cakeindex',
      });
    }
  },
  //----------------------跳转某个 新品----------------------
  toNewProducts:function(e){
    var product = e.currentTarget.dataset.product;
    var goodsid;
    if (product.chainaddress.indexOf('mcake') >=0){
      goodsid = "cake" + product.id;
      app.globalData.tempCakeIndex = goodsid;
      wx.switchTab({
        url: '../cake/cakeindex',
      });
    }
    else{
      goodsid = "bread" + product.id;;
      app.globalData.tempBreadIndex = goodsid;
      wx.switchTab({
        url: '../bread/breadindex',
      });
    }
  },
  //---------------------------跳转 砍价---------------------------------
  bargainAction: function () {
    wx.navigateTo({
      url: '../bargain/bargainindex',
    });
  },
  //-----------------------------------------------------------
  showNotice: pmd.showNotice,
  closeNotice: pmd.closeNotice
})